﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
    public partial class wfPerExpeLaboMnt : EditPage 
    {
       /// <summary>
       /// Guarda el estado de un DataTable, se usa para los grados de instruccion del personal
       /// </summary>
       /// <value></value>
       /// <returns></returns>
       public DataTable DataSourceEduca
       {
          get { return (DataTable)this.ViewState["_dsEdu"]; }
          set { this.ViewState["_dsEdu"] = value; }
       }

       /// <summary>
       /// Guarda el Indice del Registro en Edicion en un GridView
       /// </summary>
       /// <value></value>
       /// <returns></returns>
       public int EditRowIndexEdu
       {
           get { return Convert.ToInt32(this.ViewState["_erowIdxEdu"]); }
           set { this.ViewState["_erowIdxEdu"] = value.ToString(); }
       }

       public GridEditAction GridEditActionEdu
       {
           get { return (GridEditAction)this.ViewState["_gridEditAcEdu"]; }
           set { this.ViewState["_gridEditAcEdu"] = value; }
       }

       public const int _GRDLIST_STANULADO = 7;
       public const int _GRDLIST_STANULADOEDU = 9;

      protected void Page_Load(object sender, EventArgs e)
      {
           if (this.IsPostBack){
               this.LoadInPostBack();
               return;}
           this.LoadData();
      }

      public override void LoadInPostBack()
      {
          this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction) Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         this.SetInitialTitle("Personal");
      }

      public override void LoadListControls()
      {
          ETablaGenDet objEDet = new ETablaGenDet();
          ETablaPEDet objEDetPE = new ETablaPEDet();
          ECCosto objECCosto = new ECCosto();
          EEmpEstable objEEmp = new EEmpEstable();
          EBanco objEBanco = new EBanco();

          IBOQuery objBOCCosto = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CCosto));
          IBOQuery objBOBanco = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.Banco));
          MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
          RHBOQry.ITablaPE objBOIPE = (RHBOQry.ITablaPE)WCFHelper.GetObject<RHBOQry.ITablaPE>(typeof(RHBOQry.TablaPE));
          SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));

          objEDet.IdTabla = ConstMultitabla.TIPO_DOCUMENTO;
          DataTable dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidTDocIden, dt, "dsAbrev", "idCodigo", true, true);
        
          objEDet.IdTabla = ConstMultitabla.SEXO;
          dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidSexo, dt, "dsDescrip", "idCodigo", true, true);

          objEDet.IdTabla = ConstMultitabla.GRADO_DE_INSTRUCCION;
          dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidNivelEdu, dt, "dsDescrip", "idCodigo", true, true);
  
      }

      public override void DataToControls()
      {
          EPersonal objE = new EPersonal();
          IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Personal));
          objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objE.IdPersonal = Request.QueryString["IdPersonal"];
          objE = (EPersonal)objBO.GetByKey(objE);

          if (objE != null)
          {

              WebHelper.EntityToControl(objE, "IdPersonal", txtidPersonal);
              WebHelper.EntityToControl(objE, "FeNacimien", txtfeNacimiento);
              WebHelper.EntityToControl(objE, "IdSexo", ddlidSexo);

              ECliProv objECli = new ECliProv();
              IBOQuery objBOCli = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CliProv));
              objECli.IdCliProv = objE.IdCliProv;
              objECli = (ECliProv)objBOCli.GetByKey(objECli);

              if (objECli != null)
              {
                  WebHelper.EntityToControl(objECli, "IdCliProv", hdnidCliProv);
                  WebHelper.EntityToControl(objECli, "DsAPaterno", txtdsAPaterno);
                  WebHelper.EntityToControl(objECli, "DsAMaterno", txtdsAMaterno);
                  WebHelper.EntityToControl(objECli, "DsNombres", txtdsNombres);
                  WebHelper.EntityToControl(objECli, "IdTDocIden", ddlidTDocIden);
                  WebHelper.EntityToControl(objECli, "NuDocIden", txtnuDocIden);

                  WebHelper.EntityToControl(objECli, "NuTelf", txtnuTelf);
                  WebHelper.EntityToControl(objECli, "NuFax", txtnufax);
                  WebHelper.EntityToControl(objECli, "DsEmail", txtdsEmail);
                  WebHelper.EntityToControl(objECli, "DsWeb", txtdsWeb);

              }

          }

          EPerExpeLabo objEDet = new EPerExpeLabo();
          RHBOQry.IPersonal objBODet = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
          objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objEDet.IdPersonal = Request.QueryString["IdPersonal"];
          DataTable dt = objBODet.GetByParentKeyPerExpeLabo(objEDet);

          this.GridSetDataBinding(grdList, dt);

          EPerEducacion objEDetEdu = new EPerEducacion();
         
          objEDetEdu.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objEDetEdu.IdPersonal = Request.QueryString["IdPersonal"];
          DataTable dtEdu = objBODet.GetByParentKeyPerEducacion(objEDetEdu);
          this.DataSourceEduca = dtEdu;
          grdListEdu.DataSource = this.DataSourceEduca;
          grdListEdu.DataBind();

      }

 
      public override void RegisterGeneralScripts()
      {
          txtidOcupa.Attributes.Add("readonly", "true");

          StringBuilder sb = new StringBuilder();

          sb.AppendLine("var varRet = modalDialog('../MG/wfOcupacionBnd.aspx',725,400);");
          sb.AppendLine("if(varRet==null)");
          sb.AppendLine("return false;");
          sb.AppendLine("ctl = document.getElementById('" + this.hdnidOcupa.ClientID + "');");
          sb.AppendLine("ctl.value = varRet[0];");
          sb.AppendLine("ctl = document.getElementById('" + this.txtidOcupa.ClientID + "');");
          sb.AppendLine("ctl.value = varRet[1];");

          this.txtidOcupa.OnClientClick = sb.ToString();


          sb = new StringBuilder();


          sb.AppendLine("function validateEditForm(key){");
          //sb.AppendLine("var varRpta = isEmptyControl('" + txtdsAPaterno.ClientID + "','Ingrese el Apellido Paterno');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + txtdsAPaterno.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + txtdsAMaterno.ClientID + "','Ingrese el Apellido Materno');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + txtdsAMaterno.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + txtdsNombres.ClientID + "','Ingrese los Nombres');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + txtdsNombres.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + ddlidTDocIden.ClientID + "','Seleccione el tipo de Documento de Identidad');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + ddlidTDocIden.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + txtnuDocIden.ClientID + "','Ingrese el numero de Documento de Identidad');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + txtnuDocIden.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + txtfeNacimiento.ClientID + "','Seleccione o ingrese la fecha de Nacimiento');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + txtfeNacimiento.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + ddlidSexo.ClientID + "','Seleccione el Genero');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + ddlidSexo.ClientID + "').focus();return !varRpta;}");
          //sb.AppendLine("var varRpta = isEmptyControl('" + txtnuTelf.ClientID + "','Ingrese el numero de telefono');");
          //sb.AppendLine("if(varRpta==true){");
          //sb.AppendLine("document.getElementById('" + txtnuTelf.ClientID + "').focus();return !varRpta;}");
          sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
          sb.AppendLine("}");
          ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      public void RegisterEditPopupScripts()
      {
          StringBuilder sb = new StringBuilder();

          sb.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsEmpresa.ClientID + "','Ingrese Descripcion Empresa.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtidOcupa.ClientID + "','Ingrese Ocupacion.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeInicio.ClientID + "','Ingrese Fecha Inicio.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeFin.ClientID + "','Ingrese Fecha Inicio.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsDescripOcupa.ClientID + "','Ingrese Descripcion Ocupación.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("}");

          sb.AppendLine("function validate" + this.bttOkPopupEdu.ID + "(){");
          sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidNivelEdu.ClientID + "','Seleccionar Nivel.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsCodigoEdu.ClientID + "','Ingrese Codigo.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsCentroEstEdu.ClientID + "','Ingrese Centro de Estudios.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeInicioEdu.ClientID + "','Ingrese Fecha Inicio.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeFinEdu.ClientID + "','Ingrese Fecha Inicio.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeObtGradoEdu.ClientID + "','Ingrese Fecha Obtencion de Grado.');");
          sb.AppendLine("if(varRpta==true)");
          sb.AppendLine("return !varRpta;");
          sb.AppendLine("}");

          //if (this.GridEditAction == GridEditAction.Delete)
          //{
          //    sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeBajaDH.ClientID + "','Ingrese la fecha de baja.');");
          //    sb.AppendLine("if(varRpta==true)");
          //    sb.AppendLine("return !varRpta;");
          //    sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidMotivoBajaDH.ClientID + "','Selecciona el motivo de baja.');");
          //    sb.AppendLine("if(varRpta==true)");
          //    sb.AppendLine("return !varRpta;");
          //}

          //sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsUbigeo.ClientID + "','Seleccione el Ubigeo.');");
          //sb.AppendLine("if(varRpta==true)");
          //sb.AppendLine("return !varRpta;");
          //sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidTipoVia.ClientID + "','Selecciona el Tipo de via.');");
          //sb.AppendLine("if(varRpta==true)");
          //sb.AppendLine("return !varRpta;");
          //sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsNombreVia.ClientID + "','Ingrese el nombre de la via.');");
          //sb.AppendLine("if(varRpta==true)");
          //sb.AppendLine("return !varRpta;");
          //sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuVia.ClientID + "','Ingrese el Numero de la direccion.');");
          //sb.AppendLine("if(varRpta==true)");
          //sb.AppendLine("return !varRpta;");
          //sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidTipoZona.ClientID + "','Selecciona el Tipo de zona.');");
          //sb.AppendLine("if(varRpta==true)");
          //sb.AppendLine("return !varRpta;");
          //sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsNombZona.ClientID + "','Ingrese el nombre de la zona.');");
          //sb.AppendLine("if(varRpta==true)");
          //sb.AppendLine("return !varRpta;");
          //sb.AppendLine("return true;}");

          //sb.AppendLine("function onchange" + this.ddlidVincFamDH.ID + "(){");
          //sb.AppendLine("var ddl = document.getElementById('" + this.ddlidVincFamDH.ClientID + "').value;");
          //sb.AppendLine("var txt = document.getElementById('" + this.txtfeConcepDH.ClientID + "');");
          //sb.AppendLine("if( ddl == '" + ConstVinculoFamiliar.GESTANTE + "')");
          //sb.AppendLine("txt.disabled = false;");
          //sb.AppendLine("else");
          //sb.AppendLine("txt.disabled = true;");
          //sb.AppendLine("};");

          ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb.ToString(), true);

          this.bttOkPopup.OnClientClick = "return validate" + bttOkPopup.ID + "();";
          this.bttOkPopupEdu.OnClientClick = "return validate" + bttOkPopupEdu.ID + "();";
          this.bttCancelPopup.OnClientClick = "$find('" + this.mdlPopup.ClientID + "').hide();return false;";
          this.bttCancelPopupEdu.OnClientClick = "$find('" + this.mdlPopupEdu.ClientID + "').hide();return false;";
       

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if(e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if(this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break; 

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfPerExpeLaboBnd.aspx?Filter=1");
               break;
      
         }
        
      }

      protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if(e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
                 this.GridEditAction = GridEditAction.AddNew;
                 this.lblTitlePopup.Text = "Experiencia Laboral (Nuevo)";
                 this.mdlPopup.Show();
                 this.LoadDataEditPopup(grdList, -1);
                 this.RegisterEditPopupScripts();
                 break;
         }
        
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
                 this.lblTitlePopup.Text = "Experiencia Laboral (Editar)";
               this.GridEditAction = GridEditAction.Edit;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
            case ConstGridRowCommand.GRIDDELETE:
               this.lblTitlePopup.Text = "Experiencia Laboral (Dar de Baja)";
               this.GridEditAction = GridEditAction.Delete;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
         }
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            { e.Row.ForeColor = System.Drawing.Color.Red; }
         }


      }

        //Segundo Gridview

      protected void mnuGridEdu_MenuItemClick(Object sender, MenuEventArgs e)
      {
          if (e.Item == null)
              return;

          switch (e.Item.Value)
          {
              case ConstMenuToolbarValue.BUTTON_NEW:
                  this.GridEditActionEdu = GridEditAction.AddNew;
                  this.lblTitlePopup.Text = "Educacion (Nuevo)";
                  this.mdlPopupEdu.Show();
                  this.LoadDataEditPopup(grdList, -1);
                  this.RegisterEditPopupScripts();
                  break;
          }

      }

      protected void grdListEdu_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
          this.EditRowIndexEdu = Convert.ToInt32(e.CommandArgument);

          switch (e.CommandName)
          {
              case ConstGridRowCommand.GRIDEDIT:
                  this.lblTitlePopup.Text = "Educacion (Editar)";
                  this.GridEditActionEdu = GridEditAction.Edit;
                  this.mdlPopupEdu.Show();
                  this.LoadDataEditPopup(grdListEdu, this.EditRowIndexEdu);
                  this.RegisterEditPopupScripts();
                  break;
              case ConstGridRowCommand.GRIDDELETE:
                  this.lblTitlePopup.Text = "Educacion (Dar de Baja)";
                  this.GridEditActionEdu = GridEditAction.Delete;
                  this.mdlPopupEdu.Show();
                  this.LoadDataEditPopup(grdListEdu, this.EditRowIndexEdu);
                  this.RegisterEditPopupScripts();
                  break;
          }
      }

      protected void grdListEdu_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              if (e.Row.Cells[_GRDLIST_STANULADOEDU].Text == ConstFlagEstado.ACTIVADO)
              { e.Row.ForeColor = System.Drawing.Color.Red; }
          }


      }

      protected void bttOkPopup_Click(object sender, EventArgs e)
      {


          Object objResNuSec = null;
          int intResNusec = 0;
          DataTable dt = (DataTable)this.DataSource;
          DataRow dr = null;

          if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
          {
              objResNuSec = dt.Compute("MAX(nuSecuen)", "");
              if (objResNuSec == null || objResNuSec.ToString() == "")
                  intResNusec = 0;
              else
                  intResNusec = Convert.ToInt32(objResNuSec);
              intResNusec += 1;
              dr = dt.NewRow();
              dr["nuSecuen"] = intResNusec;
          }
          else
          {
              dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
          }

          if (this.GridEditAction != GridEditAction.Delete)
          {
              dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
              dr["idPersonal"] = Request.QueryString["IdPersonal"];
              dr["dsEmpresa"] = this.txtdsEmpresa.Text;
              dr["idOcupa"] = this.hdnidOcupa.Value;
              dr["dsOcupa"] = this.txtidOcupa.Text;
              dr["feInicio"] = DateTime.Parse(this.txtfeInicio.Text);
              dr["feFin"] = DateTime.Parse(this.txtfeFin.Text);
              dr["dsDescripOcupa"] = this.txtdsDescripOcupa.Text;
              dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;

          }
          else
          {
              dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
          }

          if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
          {
              dt.Rows.Add(dr);
          }


          this.GridSetDataBinding(grdList, dt);
      }


      protected void bttOkPopupEdu_Click(object sender, EventArgs e)
      {

          Object objResNuSec = null;
          int intResNusec = 0;
          DataTable dt = (DataTable)this.DataSourceEduca;
          DataRow dr = null;

          if (this.GridEditActionEdu == GridEditAction.AddNew || this.GridEditActionEdu == GridEditAction.Copy)
          {
              objResNuSec = dt.Compute("MAX(nuSecuen)", "");
              if (objResNuSec == null || objResNuSec.ToString() == "")
                  intResNusec = 0;
              else
                  intResNusec = Convert.ToInt32(objResNuSec);
              intResNusec += 1;
              dr = dt.NewRow();
              dr["nuSecuen"] = intResNusec;
          }
          else
          {
              dr = this.GridGetRowFromDataKey(this.DataSourceEduca,grdListEdu, this.EditRowIndexEdu);
          }

          if (this.GridEditActionEdu != GridEditAction.Delete)
          {
              dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
              dr["idPersonal"] = Request.QueryString["IdPersonal"];
              dr["idNivel"] = this.ddlidNivelEdu.SelectedItem.Value;
              dr["dsNivel"] = this.ddlidNivelEdu.SelectedItem.Text;
              dr["dsCodigo"] = this.txtdsCodigoEdu.Text;
              dr["dsCentroEst"] = this.txtdsCentroEstEdu.Text;
              dr["feInicio"] = DateTime.Parse(this.txtfeInicioEdu.Text);
              dr["feFin"] = DateTime.Parse(this.txtfeFinEdu.Text);
              dr["feObtGrado"] = DateTime.Parse(this.txtfeObtGradoEdu.Text);
              dr["dsObservacion"] = this.txtdsObservacionEdu.Text;
              dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;

          }
          else
          {
              dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
          }

          if (this.GridEditActionEdu == GridEditAction.AddNew || this.GridEditActionEdu == GridEditAction.Copy)
          {
              dt.Rows.Add(dr);
          }

          this.DataSourceEduca = dt;
          grdListEdu.DataSource = this.DataSourceEduca;
          grdListEdu.DataBind();
      }

      public override void LoadDataEditPopup(GridView grid, int rowIndex)
      {
          this.ClearEditPopup();
          //this.LockControlsPopup();

          if (grid == grdList)
          {
              DataRow dr = this.GridGetRowFromDataKey(grdList, rowIndex);

              if (dr == null)
                  return;

              WebHelper.RowToControl(dr, "dsEmpresa", txtdsEmpresa);
              WebHelper.RowToControl(dr, "dsOcupa", txtidOcupa);
              WebHelper.RowToControl(dr, "feInicio", txtfeInicio);
              WebHelper.RowToControl(dr, "feFin", txtfeFin);
              WebHelper.RowToControl(dr, "dsDescripOcupa", txtdsDescripOcupa);
          }

          if (grid == grdListEdu)
          {
              DataRow dr = this.GridGetRowFromDataKey(this.DataSourceEduca, grdListEdu, rowIndex);

              if (dr == null)
                  return;

              WebHelper.RowToControl(dr, "idNivel", ddlidNivelEdu);
              WebHelper.RowToControl(dr, "dsCodigo", txtdsCodigoEdu);
              WebHelper.RowToControl(dr, "dsCentroEst", txtdsCentroEstEdu);
              WebHelper.RowToControl(dr, "feInicio", txtfeInicioEdu);
              WebHelper.RowToControl(dr, "feFin", txtfeFinEdu);
              WebHelper.RowToControl(dr, "feObtGrado", txtfeObtGradoEdu);
              WebHelper.RowToControl(dr, "dsObservacion", txtdsObservacionEdu);
             
          }
          
      }

      public new void ClearEditPopup()
      {
          this.txtdsEmpresa.Text = "";
          this.txtidOcupa.Text = "";
          this.txtfeInicio.Text = "";
          this.txtfeFin.Text = "";
          this.txtdsDescripOcupa.Text = "";
          this.ddlidNivelEdu.Text = "";
          this.txtdsCodigoEdu.Text = "";
          this.txtdsCentroEstEdu.Text = "";
          this.txtfeInicioEdu.Text = "";
          this.txtfeFinEdu.Text = "";
          this.txtfeObtGradoEdu.Text = "";
          this.txtdsObservacionEdu.Text = "";
      }

      public void LockControlsPopup()
      {
          switch (this.GridEditAction)
          {
              case GridEditAction.AddNew:
              case GridEditAction.Edit:
                  
                  break;
              case GridEditAction.Delete:
                  this.txtdsEmpresa.Enabled = false;
                  this.txtidOcupa.Enabled = false;
                  this.txtfeInicio.Enabled = false;
                  this.txtfeFin.Enabled = false;
                  this.txtdsDescripOcupa.Enabled = false;
                  break;
          }
      }

      public override void SaveData(String key)
      {
          if (this.ValidateData() == false)
              return;

          IEntityBase objE = (IEntityBase)this.CreateEntitySet();
          IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));

          String[] strRet = objPro.UpdateData(objE);
          String strMsg = "";

          switch (this.FormEditAction)
          {

              case FormEditAction.AddNew:
              case FormEditAction.Copy:
                  strMsg = "Se ha registrado la Experiencia Laboral : " + strRet[0].ToString();
                  break;
              default:
                  strMsg = "Se ha modificado la Experiencia Laboral : " + txtidPersonal.Text;
                  break;
          }
          this.hdnMsgOPB.Value = this.CreatePostbackMessage("CORRECTO", strMsg, TypeMessageAlert.correct, false, "wfPerExpeLaboBnd.aspx?Filter=1");
      }

      public override void DeleteData()
      {
          IEntityBase objE = (IEntityBase)this.CreateEntitySet();
          IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
          objPro.DeleteData(objE);

          this.hdnMsgOPB.Value = this.CreatePostbackMessage("CORRECTO", "Se anulo el Registro  : " + txtidPersonal.Text, TypeMessageAlert.correct, false, "wfPerDereHabiBnd.aspx?Filter=1");

      }

      public override Object CreateEntitySet()
      {

          DataTable dt = (DataTable)this.DataSource;
          this.ClearDeletedRow(this.DataSource, "stAnulado");
          IList<EPerExpeLabo> colECerti = new List<EPerExpeLabo>();

          if (this.FormEditAction == FormEditAction.Delete)
          {
              foreach (DataRow dr in dt.Rows)
              {
                  this.MarkDeletedRow(dr, "stAnulado");
              }
          }


          for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
          {
              DataTable dtChange = null;
              if (intTypeUpd == 0)
                  dtChange = dt.GetChanges(DataRowState.Added);
              else
                  dtChange = dt.GetChanges(DataRowState.Modified);


              if (dtChange != null)
              {
                  foreach (DataRow drDet in dtChange.Rows)
                  {
                      EPerExpeLabo objEDet = DataHelper.CopyDataRowToEntity<EPerExpeLabo>(drDet, typeof(EPerExpeLabo));

                      if (drDet.RowState == DataRowState.Added)
                          objEDet.EntityState = EntityState.Added;

                      if (drDet.RowState == DataRowState.Modified)
                      {
                          if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                              objEDet.EntityState = EntityState.Modified;
                          else
                              objEDet.EntityState = EntityState.Deleted;
                      }

                      colECerti.Add(objEDet);
                  }
              }
          }


          DataTable dtEdu = (DataTable)this.DataSourceEduca;
          this.ClearDeletedRow(this.DataSourceEduca, "stAnulado");
          IList<EPerEducacion> colECertiEdu = new List<EPerEducacion>();

          if (this.FormEditAction == FormEditAction.Delete)
          {
              foreach (DataRow dr in dtEdu.Rows)
              {
                  this.MarkDeletedRow(dr, "stAnulado");
              }
          }


          for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
          {
              DataTable dtChange = null;
              if (intTypeUpd == 0)
                  dtChange = dtEdu.GetChanges(DataRowState.Added);
              else
                  dtChange = dtEdu.GetChanges(DataRowState.Modified);


              if (dtChange != null)
              {
                  foreach (DataRow drDet in dtChange.Rows)
                  {
                      EPerEducacion objEDet = DataHelper.CopyDataRowToEntity<EPerEducacion>(drDet, typeof(EPerEducacion));

                      if (drDet.RowState == DataRowState.Added)
                          objEDet.EntityState = EntityState.Added;

                      if (drDet.RowState == DataRowState.Modified)
                      {
                          if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                              objEDet.EntityState = EntityState.Modified;
                          else
                              objEDet.EntityState = EntityState.Deleted;
                      }

                      colECertiEdu.Add(objEDet);
                  }
              }
          }


          EPersonalSet objESet = new EPersonalSet();
          objESet.ColPerExpeLabo = colECerti;
          objESet.ColPerEducacion = colECertiEdu;
          

          return objESet;
      }
    
      public override void LockControls()
      {
         //if (this.FormEditAction == FormEditAction.Delete)
         //else  
      }

   }
}